package com.applink.sell_proof.urlimageviewhelper;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public final class BitmapUtils {
	public static final int CORNER_PERCENT = 14;

	private BitmapUtils() {

	}

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,
			final boolean border, boolean darker) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0xffffffff);

		final Rect rectFullSize = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		final RectF rectFFullSize = new RectF(rectFullSize);
		final float roundPx = bitmap.getWidth() * CORNER_PERCENT / 100;

		if (border) {
			final int boderWidth = Math.max(bitmap.getWidth() / 100, 1);
			final Rect rectImage = new Rect(boderWidth, boderWidth,
					bitmap.getWidth() - boderWidth, bitmap.getHeight()
							- boderWidth);
			final RectF rectFImage = new RectF(rectImage);

			canvas.drawRoundRect(rectFImage, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			if (darker) {
				paint.setColorFilter(new PorterDuffColorFilter(Color.argb(0x66,
						0, 0, 0), Mode.SRC_OVER));
			}
			canvas.drawBitmap(bitmap, rectImage, rectImage, paint);

			paint.setColorFilter(null);
			paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
			canvas.drawRoundRect(rectFFullSize, roundPx, roundPx, paint);
		} else {
			canvas.drawRoundRect(rectFFullSize, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rectFullSize, rectFullSize, paint);
		}

		return output;
	}
	
	public static Bitmap getRoundedTopCornerBitmap(Bitmap bitmap,
			final boolean border, boolean darker) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0xffffffff);

		final Rect rectFullSize = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		final RectF rectFFullSize = new RectF(rectFullSize);
		final float roundPx = bitmap.getWidth() * CORNER_PERCENT / 100;
		
		final Rect rectFullSizeTop = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight() + (int) roundPx);
		
		final RectF rectFFullSizeTop = new RectF(rectFullSizeTop);

		if (border) {
			final int boderWidth = Math.max(bitmap.getWidth() / 100, 1);
			final Rect rectImage = new Rect(boderWidth, boderWidth,
					bitmap.getWidth() - boderWidth, bitmap.getHeight()
							- boderWidth);
			final RectF rectFImage = new RectF(rectImage);

			canvas.drawRoundRect(rectFImage, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			if (darker) {
				paint.setColorFilter(new PorterDuffColorFilter(Color.argb(0x66,
						0, 0, 0), Mode.SRC_OVER));
			}
			canvas.drawBitmap(bitmap, rectImage, rectImage, paint);

			paint.setColorFilter(null);
			paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
			canvas.drawRoundRect(rectFFullSize, roundPx, roundPx, paint);
		} else {
			canvas.drawRoundRect(rectFFullSizeTop, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rectFullSize, rectFullSize, paint);
		}

		return output;
	}
}
